package com.cndw.kungfu.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.cndw.kungfu.comm.AccessFlag;
import com.cndw.kungfu.config.ConstantsFlag;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.config.ConstantsLevel;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.config.RMK;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.player.domain.Dragon;
import com.cndw.kungfu.model.player.domain.Favorite;
import com.cndw.kungfu.model.player.domain.SortFavorite;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.kungfu.vo.FavoriteHFVO;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

/**
 * 一些角色扩展功能
 * 
 * @author fantadust
 * 
 */
@Controller
public class PlayerExtController {
	/**
	 * 龙纹显示界面
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage dragonShow(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		int lw = player.getDataInfo().intDataGet(ConstantsIntKey.lw);
		int lwe = player.getDataInfo().intDataGet(ConstantsIntKey.lwe);

		Dragon dragon = Dragon.getOne(lw);
		Dragon dragon2 = Dragon.getOne(lw + 1);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("lw", lw);
		map.put("lwe", lwe);
		map.put("lweNeed", dragon2 != null ? dragon2.getUpgradeExp() : 0);
		map.put("adds", dragon != null ? dragon.getAddsVar() : null);
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 龙纹培养
	 * 
	 * @param session
	 * @param type
	 * @param batch
	 * @return
	 */
	public ResponseMessage dragonFeed(Session session, int type, boolean batch) {
		int[] cost = ConstantsModel.lwGoodsId[type];
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getLevel() < ConstantsLevel.lv50lw) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.level", ConstantsLevel.lv50lw));
		}
		int lw = player.getDataInfo().intDataGet(ConstantsIntKey.lw);
		int lwe = player.getDataInfo().intDataGet(ConstantsIntKey.lwe);
		if (lw >= ConstantsModel.lwMaxLevel) {
			return null;
		} else if (!player.getPlayerBoxs().isHaveGoods(cost[0])) {
			return null;
		}

		int upTopNeed = 0;
		if (batch) {
			Dragon dragonUp = Dragon.getOne(lw + 1);
			while (dragonUp != null && dragonUp.getLevel() <= ConstantsModel.lwMaxLevel) {
				upTopNeed += dragonUp.getUpgradeExp();
				dragonUp = Dragon.getOne(dragonUp.getLevel() + 1);
			}
			upTopNeed -= lwe;
		}

		int expGot = 0, sn = 0, bn = 0;
		boolean smallCrit = false, bigCrit = false, upgrade = false;
		int haveNum = batch ? player.getPlayerBoxs().getStackNum(cost[0]) : 1;
		int costNum = 0;
		for (int n = 0; n < haveNum; n++) {
			for (int i = 0; i < cost[1]; i++) {
				bigCrit = NumberProcesser.isWinning(cost[3]);
				smallCrit = NumberProcesser.isWinning(cost[2]);
				if (bigCrit) {
					expGot += ConstantsModel.lwExpGot * 10;
					bn++;
				} else if (smallCrit) {
					expGot += ConstantsModel.lwExpGot * 3;
					sn++;
				} else {
					expGot += ConstantsModel.lwExpGot;
				}
			}

			costNum++;
			if (!batch || expGot >= upTopNeed) {
				break;
			}
		}

		player.getPlayerBoxs().costGoods(cost[0], costNum);

		lwe += expGot;
		Dragon dragonNext = Dragon.getOne(lw + 1);
		while (dragonNext != null && lwe >= dragonNext.getUpgradeExp()) {
			lwe -= dragonNext.getUpgradeExp();
			lw++;
			upgrade = true;
			dragonNext = Dragon.getOne(lw + 1);
		}
		Dragon dragon = Dragon.getOne(lw);
		if (upgrade) {
			player.getDataInfo().intDataSet(ConstantsIntKey.lw, lw);
			player.clacProperties();
		}
		player.getDataInfo().intDataSet(ConstantsIntKey.lwe, lwe);
		player.getDataInfo().commitSync();

		GoodsMode goodsMode = Context.getGoodsServer().getModeById(cost[0]);
		LogServer.recordLog(player, ConstantsLog.playerExt11, 110001, goodsMode.getName(), costNum, expGot, upgrade, lw, lwe, batch);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sn", sn);
		map.put("bn", bn);
		map.put("expGot", expGot);
		map.put("lw", lw);
		map.put("lwe", lwe);
		map.put("upgrade", upgrade);
		if (upgrade) {
			map.put("player", player.toPlayerVO());
			map.put("lweNeed", dragonNext == null ? 0 : dragonNext.getUpgradeExp());
			map.put("adds", dragon.getAddsVar());
		}

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 收藏系统
	 * 
	 * @param session
	 * @param type
	 * @return
	 */
	public ResponseMessage favorite(Session session, int type) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());

		Map<String, Object> map = new HashMap<String, Object>();
		if (type == 1) {
			int fhf = player.getDataInfo().intDataGet(ConstantsIntKey.fhf);
			ArrayList<FavoriteHFVO> favoriteHFVOs = new ArrayList<FavoriteHFVO>();
			Collection<Favorite> favorites = Favorite.getAll();
			for (Favorite e : favorites) {
				if (e.getType() != type) {
					continue;
				}
				boolean act = (fhf & e.getSg()) > 0;
				favoriteHFVOs.add(e.toVOHF(act));
			}
			Collections.sort(favoriteHFVOs, new SortFavorite());

			map.put("fhf", player.getDataInfo().intDataGet(ConstantsIntKey.fhf));
			map.put("fhfv", player.getDataInfo().intDataGet(ConstantsIntKey.fhfv));
			map.put("list", favoriteHFVOs);
		} else {

		}

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 进行收藏
	 * 
	 * @param session
	 * @param indexId
	 * @return
	 */
	@AccessFlag(ConstantsFlag.fucNewChk)
	public ResponseMessage favoriteDo(Session session, int id) {
		Favorite favorite = Favorite.getOne(id);
		if (favorite == null) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		int fhf = player.getDataInfo().intDataGet(ConstantsIntKey.fhf);
		if ((fhf & favorite.getSg()) > 0) {
			return null;
		}
		if (!player.getPlayerBoxs().isHaveGoods(favorite.getGoodsId())) {
			return null;
		}
		player.getPlayerBoxs().costGoods(favorite.getGoodsId(), 1);
		fhf |= favorite.getSg();
		int fhfv = 0 ;
		Collection<Favorite> favorites = Favorite.getAll();
		for (Favorite e : favorites) {
			if (e.getType() == 1 && (e.getSg() & fhf)>0){
				fhfv += e.getAddV();
			}
		}
		

		player.getDataInfo().intDataSet(ConstantsIntKey.fhf, fhf);
		player.getDataInfo().intDataSet(ConstantsIntKey.fhfv, fhfv);
		player.getDataInfo().commitSync();
		player.clacProperties();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("fhf", fhf);
		map.put("fhfv", fhfv);
		map.put(RMK.player, player.toPlayerVO());
		GoodsMode goodsMode = Context.getGoodsServer().getModeById(favorite.getGoodsId());

		LogServer.recordLog(player, ConstantsLog.playerExt11, 110011, goodsMode.getName(), favorite.getId(), fhf, fhfv);

		return ResponseMessage.getSuccMessage(map);
	}

}
